package org.lsda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsda.sched.LsdaScheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;

import com.google.gson.JsonObject;

/**
 * Servlet implementation class UpdateTrigger
 */
@WebServlet("/UpdateTrigger")
public class UpdateTrigger extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateTrigger() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		JsonObject json = new JsonObject();
		json.addProperty("Result", "OK");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String jobName = request.getParameter("JobName");
		String jobGroup = request.getParameter("JobGroup");
		String triggerName = request.getParameter("TriggerName");
		String triggerGroup = request.getParameter("TriggerGroup");
		int schedType = Integer.parseInt(request.getParameter("SchedType"));
		String s = request.getParameter("Start").trim();
		String e = request.getParameter("End").trim();
		Date start = null;
		Date end = null;
		try {
			if (!s.equals(""))
				start = sdf.parse(request.getParameter("Start").trim());
			if (!e.equals(""))
				end = sdf.parse(request.getParameter("End").trim());
		} catch (ParseException ex) {
			ex.printStackTrace();
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", ex.getMessage());
			PrintWriter writer = response.getWriter();
//			System.out.println(json.toString());
			writer.write(json.toString());
			return;
		}

		int repeat = Integer.parseInt(request.getParameter("Repeat"));
		int interval = Integer.parseInt(request.getParameter("Interval"));
		String crontab = request.getParameter("Crontab");
		String description = request.getParameter("TriggerDesc");

		try {
			LsdaScheduler.updateTrigger(jobName, jobGroup, triggerName,
					triggerGroup, schedType, start, end, repeat, interval,
					crontab, description);
		} catch (SchedulerException e1) {
			// TODO Auto-generated catch block
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", e1.getMessage());
			e1.printStackTrace();
		}

		PrintWriter writer = response.getWriter();
//		System.out.println(json.toString());
		writer.write(json.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
//		Enumeration<String> es = request.getParameterNames();
//
//		while (es.hasMoreElements()) {
//			String name = es.nextElement();
//			System.out.println("updatetrigger " + name + "="
//					+ request.getParameter(name));
//		}
	}

}
